package com.nian.db.mysql.mycat.simple.service.impl;

import com.nian.common.base.util.StringUtils;
import com.nian.db.mysql.mycat.simple.dao.MyCatUserRepository;
import com.nian.db.mysql.mycat.simple.model.MyCatUser;
import com.nian.db.mysql.mycat.simple.service.IMyCatUserService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用途：
 * 开发：zhangmj
 * 日期：2020/4/16 0016 16:28
 */
@Service
public class MyCatUserServiceImpl implements IMyCatUserService {


    @Autowired
    private MyCatUserRepository myCatUserRepository;


    @Override
    public MyCatUser findById(Long id) {
        if(id != null && id > 0){
            return myCatUserRepository.findById(id).orElse(null);
        }
        return null;
    }

    @Override
    public MyCatUser save(MyCatUser model) {
        if(model != null){
            if(StringUtils.isEmpty(model.getId())){
                model.setId(IMyCatUserService.ID_SEQ);
            }
            return myCatUserRepository.save(model);
        }
        return model;
    }

    @Override
    public int insert(MyCatUser model) {
        if(model != null){
            model.setId(IMyCatUserService.ID_SEQ);
            return myCatUserRepository.insert(model);
        }
        return 0;
    }

    @Override
    public List<MyCatUser> saveAll(List<MyCatUser> list) {
        if(CollectionUtils.isNotEmpty(list)){
            return myCatUserRepository.saveAll(list);
        }
        return null;
    }

    @Override
    public void deleteById(Long id) {
        if(id != null && id > 0){
            myCatUserRepository.deleteById(id);
        }
    }
}
